package com.his.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.admin.service.MedicineService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Medicine;
import com.his.dao.domain.vo.ListVo;
import com.his.dao.mapper.MedicineMapper;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
@AllArgsConstructor
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements MedicineService {
    private MedicineMapper medicineMapper;

    @Override
    public RestResult all(ListDto listDto) {
        Medicine medicine = MapUtil.conversionMapToObj((Map) listDto.getObj(), Medicine.class);
//        IPage iPage = new Page(listDto.getPage(), listDto.getPageSize());
//        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<Medicine>()
//                .like(medicine.getName() != null, "name", medicine.getName())
//                .like(medicine.getMedicineId() != null, "medicine_id",medicine.getMedicineId())
//                .eq(medicine.getInventory() != null, "inventory",medicine.getInventory())
//                .eq(medicine.getPrice() != null, "price",medicine.getPrice())
//                .ge(medicine.getCreateTime() != null, "create_time",medicine.getCreateTime())
//                .le(medicine.getUpdateTime() != null,"update_time",medicine.getUpdateTime());
//        IPage medicinePage = medicineMapper.selectPage(iPage, queryWrapper);
//
//        return new RestResultBuilder().success(ListVo
//                .builder()
//                .list(medicinePage.getRecords())
//                .total(medicinePage.getTotal())
//                .build());

        return new RestResultBuilder().success(ListVo
                .builder()
                .list(medicineMapper.selectAll(
                        medicine.getName(),
                        medicine.getHospitalId(),
                        listDto.getPage() * listDto.getPageSize(),
                        listDto.getPageSize()))
                .total(medicineMapper.selectAllCount(
                        medicine.getName(),
                        medicine.getHospitalId()
                ))
                .build());
    }

}
